#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "oled_ssd1306.h"
#include "environment_task.h"

void oled_task_init(void){
    GpioInit();
    OledInit();
}


static void oled_task_run(void *arg){
    (void)arg;
    oled_task_init();
    OledFillScreen(0x00);

    OledShowString(0,0,"Hell,guilin college",FONT6x8);
    sleep(1);
    char line[32] = {0};
    OledFillScreen(0x00);
    while (1)
    {
        OledShowString(16,0,"Enviroment",1);
        snprintf(line,sizeof(line),"temp:%.1f deg",temperature);
        OledShowString(0,2,line,1);

        snprintf(line,sizeof(line),"humi:%1.f%%",humidity);
        OledShowString(0,3,line,1);

        snprintf(line,sizeof(line),"gas:%4d ppm",gas);
        OledShowString(0,4,line,1);

        snprintf(line,sizeof(line),"stats:%2d ",stat);
        OledShowString(0,5,line,1);
        usleep(500000);
    }
    
}

void oled_task(void){
    osThreadAttr_t attr;
    attr.name = "OledTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;

    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 4096;
    attr.priority = osPriorityNormal;


    if (osThreadNew((osThreadFunc_t) oled_task_run,NULL,&attr)==NULL)
    {
        printf("oled_task create failed.\n");
    }
}

//SYS_RUN(oled_task);